# 获取未来 40 天的天气
import json

from getInfoByRequest import getInfoByRequest
from pandasData import pandasData


class getFutureWeather:

    # 城市代码
    citytoCode = {
        "北京": "54511",
        "上海": "58367",
        "天津": "54517",
        "重庆": "57516",
        "哈尔滨": "50953",
        "长春": "54161",
        "沈阳": "54342",
        "呼和浩特": "53463",
        "石家庄": "53698",
        "太原": "53772",
        "郑州": "57083",
        "济南": "54823",
        "南京": "58238",
        "杭州": "58457",
        "合肥": "58321",
        "福州": "58847",
        "南昌": "58606",
        "长沙": "57687",
        "广州": "59287",
        "南宁": "59432",
        "海口": "59758",
        "成都": "56294",
        "贵阳": "57816",
        "昆明": "56778",
        "拉萨": "55591",
        "西安": "57036",
        "兰州": "52889",
        "银川": "53614",
        "西宁": "52866",
        "乌鲁木齐": "51463",
    }

    def __init__(self):
        pass

    # 计算未来40天每天的评分
    @staticmethod
    def calculate_weather_score(weather_data):
        # 根据天气数据打分
        score = 0

        # 天气情况评分
        if "晴" in weather_data["whole_wea"]:
            score += 20
        elif "多云" in weather_data["whole_wea"]:
            score += 15
        elif "阴" in weather_data["whole_wea"]:
            score += 10
        elif "雨" in weather_data["whole_wea"]:
            score += 5
        else:
            score += 0

        # 气温评分
        day_temp = int(weather_data["day_temp"])
        night_temp = int(weather_data["night_temp"])
        if 20 <= day_temp <= 30:
            score += 20
        elif 15 <= day_temp < 20 or 30 < day_temp <= 35:
            score += 10
        else:
            score += 0

        if 20 <= night_temp <= 30:
            score += 20
        elif 15 <= night_temp < 20 or 30 < night_temp <= 35:
            score += 10
        else:
            score += 0

        # 空气质量评分
        if weather_data["aqi_level"] == "1":
            score += 20
        elif weather_data["aqi_level"] == "2":
            score += 10
        else:
            score += 0

        # 湿度评分
        humidity = int(weather_data["humidity"].replace("%", ""))
        if 30 <= humidity <= 70:
            score += 10
        else:
            score += 0

        # 风力评分
        day_wind_level = int(weather_data["day_wind_level"].replace("级", ""))
        night_wind_level = int(weather_data["night_wind_level"].replace("级", ""))
        if 1 <= day_wind_level <= 3:
            score += 10
        else:
            score += 0

        if 1 <= night_wind_level <= 3:
            score += 10
        else:
            score += 0

        # 能见度评分
        visibility = float(weather_data["visibility"].replace("km", ""))
        if visibility > 10:
            score += 10
        else:
            score += 0

        # 紫外线评分
        if weather_data["ultraviolet"] in ["最弱", "弱"]:
            score += 10
        else:
            score += 0

        return score

    @staticmethod
    def analyze_weather_data(weather_data_list):
        # 分析天气数据列表，返回日期和对应分数的字典
        scores = {}
        for data in weather_data_list:
            date = data["date"]
            score = getFutureWeather.calculate_weather_score(data)
            scores[date] = score
        return scores

    # 获取未来40天的天气
    @staticmethod
    def getFutureWeather():
        # 解析数据
        per_city_score_data_dict = {}   # 键名是城市名 键值是该城市未来 15 天的分数
        for (city , code) in getFutureWeather.citytoCode.items():
            # 构造 url
            url = f'https://tianqi.2345.com/Pc/apiGet15Days?area_id={code}&area_type=2'
            json_data = getInfoByRequest.getFutureWeather(url)['data']
            day_score_list = getFutureWeather.analyze_weather_data(json_data)
            # 获取 1 ~ 15 天的评分列表
            day_score = day_score_list.values()
            per_city_score_data_dict[city] = list(day_score)
        pandasData().analyze_weather_data(per_city_score_data_dict)


if __name__ == '__main__':
    getFutureWeather.getFutureWeather()
